import sys
import os

pwd = os.path.dirname(os.path.abspath(__file__))
sys.path.append(pwd + '../')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mxshop.settings')

import django
django.setup()

from goods import Goods, GoodsCategory, GoodsImage
from db_tools.data.product_data import row_data

for row in row_data:
    goods = Goods()
    goods.name = row['name']
    goods.market_price = float(row['market_price'].replace("￥", "").replace("元", ""))
    goods.shop_price = float(row['sale_price'].replace("￥", "").replace("元", ""))
    goods.goods_brief = row.get("desc") if row.get("desc") else ""
    goods.goods_desc = row.get("goods_desc") if row.get("goods_desc") else ""
    goods.goods_front_image = row["images"][0]

    goods_category = GoodsCategory.objects.filter(name=row['categorys'][-1])
    if goods_category:
        goods.category = goods_category[0]
    goods.save()

    for image in row["images"]:
        goods_image = GoodsImage()
        goods_image.goods = goods
        goods_image.image = image
        goods_image.save()


goods_category = GoodsCategory.objects.all()
print(goods_category)